home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / iflib / scanout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-03  |  5.0 KB  |  233 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "xutil.h"
  7. #include "config.h"
  8. #include "ftn.h"
  9. #include "scanout.h"
  10. #include "lutil.h"
  11.  
  12. #ifndef PATH_MAX
  13. #define PATH_MAX 512
  14. #endif
  15.  
  16. typedef struct _strl {
  17.     struct _strl *next;
  18.     char *str;
  19.     char *dom;
  20.     int zone;
  21. } strl;
  22.  
  23. static faddr addr = {
  24.     NULL,
  25.     0,0,0,0,
  26.     NULL
  27. };
  28.  
  29. static int scan_dir(_each_fn_t*,char*,int);
  30. static int scan_dir(fn,dname,ispoint)
  31. _each_fn_t *fn;
  32. char *dname;
  33. int ispoint;
  34. {
  35.     int rc=0;
  36.     char fname[PATH_MAX];
  37.     char flavor='?';
  38.     DIR *dp=NULL;
  39.     struct dirent *de;
  40.     int isflo;
  41.  
  42.     debug(7,"enter scan_dir \"%s\" (%s)",dname,ispoint?"point":"node");
  43.  
  44.     if ((dp=opendir(dname)) == NULL)
  45.     {
  46.         debug(7,"outbound \"%s\" cannot be opened, proceed",dname);
  47.         return 0;
  48.     }
  49.  
  50.     while ((de=readdir(dp)))
  51.     if ((strlen(de->d_name) == 12) &&
  52.         (de->d_name[8] == '.') &&
  53.         (strspn(de->d_name,"0123456789abcdefABCDEF") == 8))
  54.     {
  55.         debug(7,"checking: \"%s\"",de->d_name);
  56.         addr.point=0;
  57.         strncpy(fname,dname,PATH_MAX-2);
  58.         strcat(fname,"/");
  59.         strncat(fname,de->d_name,PATH_MAX-strlen(fname)-2);
  60.         if ((strcasecmp(de->d_name+9,"pnt") == 0) && !ispoint)
  61.         {
  62.             sscanf(de->d_name,"%04x%04x",&addr.net,&addr.node);
  63.             if ((rc=scan_dir(fn,fname,1))) goto exout;
  64.         }
  65.         else if ((strcasecmp(de->d_name+9,"out") == 0) ||
  66.              (strcasecmp(de->d_name+9,"cut") == 0) ||
  67.              (strcasecmp(de->d_name+9,"hut") == 0) ||
  68.              (strcasecmp(de->d_name+9,"flo") == 0) ||
  69.              (strcasecmp(de->d_name+9,"clo") == 0) ||
  70.              (strcasecmp(de->d_name+9,"hlo") == 0) ||
  71.              (strcasecmp(de->d_name+9,"req") == 0))
  72.         {
  73.             if (ispoint)
  74.                 sscanf(de->d_name,"%08x",
  75.                     &addr.point);
  76.             else
  77.                 sscanf(de->d_name,"%04x%04x",
  78.                     &addr.net,&addr.node);
  79.             flavor=tolower(de->d_name[9]);
  80.             if (flavor == 'f') flavor='o';
  81.             if (strcasecmp(de->d_name+10,"lo") == 0) isflo=OUT_FLO;
  82.             else isflo=OUT_PKT;
  83.             debug(7,"%s \"%s\"",
  84.                 (isflo == OUT_FLO) ? "flo file" : "packet",
  85.                 de->d_name);
  86.             if ((rc=fn(&addr,flavor,isflo,fname))) goto exout;
  87.         }
  88.         else if ((strncasecmp(de->d_name+9,"su",2) == 0) ||
  89.                  (strncasecmp(de->d_name+9,"mo",2) == 0) ||
  90.                  (strncasecmp(de->d_name+9,"tu",2) == 0) ||
  91.                  (strncasecmp(de->d_name+9,"we",2) == 0) ||
  92.                  (strncasecmp(de->d_name+9,"th",2) == 0) ||
  93.                  (strncasecmp(de->d_name+9,"fr",2) == 0) ||
  94.                  (strncasecmp(de->d_name+9,"sa",2) == 0))
  95.         {
  96.             isflo=OUT_ARC;
  97.             if ((rc=fn(&addr,flavor,isflo,fname))) goto exout;
  98.             debug(7,"arcmail file \"%s\"",de->d_name);
  99.         }
  100.         else
  101.         {
  102.             debug(7,"skipping \"%s\"",de->d_name);
  103.         }
  104.     }
  105.  
  106. exout:
  107.     closedir(dp);
  108.     return rc;
  109. }
  110.  
  111. int scanout(fn)
  112. _each_fn_t *fn;
  113. {
  114.     int rc=0;
  115.     char fname[PATH_MAX];
  116.     fa_list *dom;
  117.     strl *doml=NULL,*tmp,**tmpd;
  118.     char *p,*q;
  119.     DIR *dp;
  120.     struct dirent *de;
  121.  
  122.     strncpy(fname,outbound,PATH_MAX-1);
  123.     if ((p=strrchr(fname,'/'))) *p='\0';
  124.     else fname[0]='\0';
  125.  
  126.     if ((dp=opendir(fname)) == NULL)
  127.     {
  128.         logerr("$cannot open outbound directory \"%s\" for reading",
  129.             fname);
  130.         return 1;
  131.     }
  132.  
  133.     for (dom=nllist;dom;dom=dom->next)
  134.     {
  135.         debug(7,"checking domain %s",dom->addr->domain);
  136.         if ((p=dom->addr->domain) == NULL) p=whoami->addr->domain;
  137.         q=p;
  138.         if ((p == NULL) || (whoami->addr->domain == NULL) ||
  139.             (strcasecmp(p,whoami->addr->domain) == 0))
  140.         {
  141.             if (dom->addr->zone == 0)
  142.                 dom->addr->zone=whoami->addr->zone;
  143.             if ((p=strrchr(outbound,'/'))) p++;
  144.             else p=outbound;
  145.         }
  146.         for (tmpd=&doml;*tmpd;tmpd=&((*tmpd)->next))
  147.             if (strcasecmp(p,(*tmpd)->str) == 0) break;
  148.         if (*tmpd == NULL)
  149.         {
  150.             *tmpd=(strl*)xmalloc(sizeof(strl));
  151.             (*tmpd)->next=NULL;
  152.             (*tmpd)->str=p;
  153.             (*tmpd)->dom=q;
  154.             (*tmpd)->zone=dom->addr->zone;
  155.         }
  156.     }
  157.  
  158.     while ((de=readdir(dp)))
  159.     for (tmpd=&doml;*tmpd;tmpd=&((*tmpd)->next))
  160.     {
  161.         debug(7,"checking directory %s against %s",
  162.             de->d_name,(*tmpd)->str);
  163.         if ((strncasecmp(de->d_name,(*tmpd)->str,strlen((*tmpd)->str)) == 0) &&
  164.             ((*(p=de->d_name+strlen((*tmpd)->str)) == '\0') ||
  165.              ((*p == '.') && (strlen(p+1) == 3) && 
  166.               (strspn(p+1,"01234567890abcdefABCDEF") == 3))))
  167.         {
  168.             if (*p) sscanf(p+1,"%03x",&addr.zone);
  169.             else addr.zone=(*tmpd)->zone;
  170.             strncpy(fname,outbound,PATH_MAX-2);
  171.             if ((p=strrchr(fname,'/'))) *p='\0';
  172.             else p=fname+strlen(fname);
  173.             strcat(fname,"/");
  174.             strncat(fname,de->d_name,PATH_MAX-strlen(fname)-2);
  175.             addr.domain=(*tmpd)->dom;
  176.             if ((rc=scan_dir(fn,fname,0))) goto exout;
  177.         }
  178.         else
  179.         {
  180.             debug(7,"skipping \"%s\"",de->d_name);
  181.         }
  182.     }
  183.  
  184. exout:
  185.     closedir(dp);
  186.     for (tmp=doml;tmp;tmp=doml)
  187.     {
  188.         doml=tmp->next;
  189.         free(tmp);
  190.     }
  191.     return rc;
  192. }
  193.  
  194. #ifdef TESTING
  195.  
  196. extern char *configname;
  197. extern int readconfig();
  198.  
  199. _each_fn_t each;
  200.  
  201. int main(argc,argv)
  202. int argc;
  203. char *argv[];
  204. {
  205.     int rc;
  206.  
  207.     if (argv[1]) configname=argv[1];
  208.     else configname="config";
  209.     if ((rc=readconfig()))
  210.     {
  211.         debug(0,"readconfig failed - %d",rc);
  212.         return rc;
  213.     }
  214.     verbose=1<<6;
  215.  
  216.     rc=scanout(each);
  217.     debug(0,"scanout rc=%d",rc);
  218.     return rc;
  219. }
  220.  
  221. int each(addr,flavor,isflo,name)
  222. faddr *addr;
  223. char flavor;
  224. int isflo;
  225. char *name;
  226. {
  227.     debug(0,"addr \"%s\" flavor '%c', %s, name \"%s\"",
  228.         ascfnode(addr,0xff),flavor,isflo?"flo":"pkt",name);
  229.     return 0;
  230. }
  231.  
  232. #endif
  233.